package com.owo.springboot.controller;

import com.owo.springboot.dto.*;
import com.owo.springboot.entity.Reservation;
import com.owo.springboot.repository.ReservationRepository;
import com.owo.springboot.service.ReservationService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class ReservationController {

    private final ReservationService reservationService;

    @PostMapping("/upload/image")
    public ResponseEntity<UploadResponse> uploadImage(
            @RequestPart("image") MultipartFile image) {

        try {
            // 上传图片
            String imageUrl = reservationService.uploadImage(image);

            CarPlateResult plateResult = reservationService.recognizePlate(image);
            String plateNumber = plateResult.getPlateNumber();

            // 返回成功响应
            return ResponseEntity.ok(new UploadResponse(imageUrl, plateNumber, true, "图片上传成功"));

        } catch (IOException e) {
            // 上传失败
            return ResponseEntity.status(500)
                    .body(new UploadResponse(null, null, false, "图片上传失败: " + e.getMessage()));
        }
    }

    @PostMapping("/reservation")
    public ResponseEntity<ReservationInfoResponse> reservate(@RequestBody ReservationInfoRequest request) {
        return ResponseEntity.ok(reservationService.reservate(request));
    }

    @GetMapping("/getParkStatus")
    public ResponseEntity<?> getParkStatus() {
        return ResponseEntity.ok(reservationService.getParkStatus());
    }

    @GetMapping("/getAllReservationId")
    public List<String> getAllReservationId(@RequestParam("userId") String userId) {
        return reservationService.findAllIdsByUserId(userId);
    }

    @GetMapping("/getAllReservation")
    public List<Reservation> getAllReservation(@RequestParam("userId") String userId) {
        return reservationService.getAllReservation(userId);
    }

    @PostMapping("/saveParkIdandPrice")
    public Reservation endOrder(@RequestBody EndOrderRequest request) {
        return reservationService.endOrder(request);
    }
}
